<template>
  <div id="order" class="container">

    <el-tabs v-model="activeName" class="tabs" type="card" @tab-click="handleClick">
      <el-tab-pane
        v-for="item in langList"
        :key="item.id"
        :label="item.lang"
        :name="item.lang"
      />
    </el-tabs>
    <div class="content">
      <div class="top">
        <h3 class="title"><span />栏目内容管理</h3>
        <div class="topbtn">
          <el-button
            type="primary"
            class="orange"
            size="mini"
            icon="el-icon-plus"
            style="margin-left:15px"
            @click="goAdd"
          >新增内容</el-button>
          <el-dropdown trigger="click" @command="handleCommand">
            <span class="el-dropdown-link el-icon-paperclip">
              查看副表
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-for="item in moduleData"
                :key="item.id"
                :command="item.id"
              >
                {{ item.title }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <div class="flex">
        <div class="tree">
          <h4>栏目列表</h4>
          <el-tree
            ref="myTree"
            :data="columnTree"
            show-checkbox
            node-key="id"
            :props="defaultProps"
            default-expand-all
            :default-checked-keys="defaultKeys"
            @check="getNode"
          />
        </div>
        <div style="width:calc(100% - 320px)">
          <el-table
            :data="contentData"
            border
            stripe
            :header-cell-style="{'text-align':'center'}"
            :cell-style="{'text-align':'center'}"
          >
            <el-table-column label="ID" prop="id" width="60px" />
            <el-table-column label="标题" prop="title" show-overflow-tooltip width="450px" />
            <el-table-column label="缩略图" prop="thumbnail">
              <template slot-scope="scope">
                <el-image
                  style="width: 50px; height: 50px"
                  :src="scope.row.thumbnail?scope.row.thumbnail.url:' '"
                  :preview-src-list="scope.row.list"
                />
              </template>
            </el-table-column>
            <el-table-column label="浏览量" prop="hits" width="70px" />
            <el-table-column label="是否置顶" prop="is_top" width="120px">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.is_top"
                  :active-value="1"
                  :inactive-value="2"
                  active-text="是"
                  inactive-text="否"
                  active-color="#f84311"
                  inactive-color="rgba(0,0,0,0.2)"
                  @change="topOpt(scope.row)"
                />
              </template>
            </el-table-column>
            <el-table-column label="是否推荐" prop="is_recommend" width="120px">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.is_recommend"
                  :active-value="1"
                  :inactive-value="2"
                  active-text="是"
                  inactive-text="否"
                  active-color="#f84311"
                  inactive-color="rgba(0,0,0,0.2)"
                  @change="recommend(scope.row)"
                />
              </template>
            </el-table-column>
            <el-table-column label="排序" prop="sort" width="60px" />
            <el-table-column label="状态" width="150px">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.status"
                  :active-value="1"
                  :inactive-value="2"
                  active-text="正常"
                  inactive-text="禁用"
                  active-color="#f84311"
                  inactive-color="rgba(0,0,0,0.2)"
                  @change="status(scope.row)"
                />
              </template>
            </el-table-column>
            <!-- <el-table-column label="发布时间" prop="push_time" /> -->
            <el-table-column label="操作" fixed="right" width="150px">
              <template slot-scope="scope">
                <el-button class="edit" size="mini" @click="editor(scope.row)">编辑</el-button>
                <el-button class="dele" size="mini" @click="del(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page="page.currentPage"
            :page-sizes="page.pageSizes"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            :hide-on-single-page="page.total<page.size?true:false"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>

      </div>
    </div>

  </div>
</template>
<script>

import websiteApi from '@/api/website.js'
import columnApi from '@/api/column.js'
import contentApi from '@/api/module'
import websetApi from '@/api/websetting'
import log from '@/api/log'
export default {
  data() {
    return {
      list: [],
      options: [], // 站点
      contentData: [], // 内容
      columnTree: [], // 栏目树形数据
      moduleData: '',
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      defaultKeys: [], // 默认展示的节点
      checkedKeys: [],
      searchForm: {
        website_id: ''

      },
      page: {
        total: 0,
        currentPage: 1,
        size: 10,
        pageSizes: [10, 20, 30, 40]
      }, // 分页器
      val: {

      },
      totalAmount: '',
      category_id: '',
      activeName: '',
      langList: [],
      node: []// 勾选的节点
    }
  },
  computed: {
    siteId1() {
      return this.$store.getters.siteId
    }
  },
  watch: {
    siteId1(val, value) {
      this.siteId = val

      this.getLang()
    }
  },
  created() {
    // this.getList()
    // this.getCateList()
    // this.getWeblist()
    this.siteId = this.$store.getters.siteId
    this.getModule()
    this.getContent()
    this.getLang()
  },
  mounted() {

  },
  updated() {

  },
  methods: {
    topOpt(row) {
      const val = {
        id: row.id,
        is_top: row.is_top
      }
      columnApi.topOpt(val).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    recommend(row) {
      const val = {
        id: row.id,
        is_recommend: row.is_recommend,
        type: 'recommend'
      }
      columnApi.recommendAndStatus(val).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    status(row) {
      const val = {
        id: row.id,
        status: row.status,
        type: 'status'
      }
      columnApi.recommendAndStatus(val).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 获取站点语言
    getLang() {
      websetApi.langInfo({ site_id: this.siteId }).then(res => {
        this.langList = res.data

        if (res.data.length > 0) {
          this.activeName = res.data[0].lang
        }

        this.getColumnlist()
      })
    },
    // tab切换
    handleClick(val) {
      //
      this.activeName = val.name
      this.getColumnlist()
    },
    // 获取站点列表
    // getWeblist() {
    //   websiteApi.websiteList().then(res => {
    //     this.options = res.data
    //     this.siteId = res.data[0].id
    //     this.getLang()
    //     // this.handleChange()
    //   })
    // },
    // 获取站点下的栏目列表
    getColumnlist() {
      const val = {
        website_id: this.siteId,
        lang: this.activeName
      }
      columnApi.columnList(val).then(res => {
        this.columnTree = res.data

        // this.defaultKeys = []
        // this.defaultKeys.push(res.data[0].id)

        this.getNode()
      })
    },
    // 获取树节点
    getNode(val, value) {
      this.$nextTick(() => {
        const checked = this.$refs.myTree.getCheckedKeys()

        // this.getContent(checked[1])
        if (value) {
          this.node = value.checkedNodes
        }

        this.category_id = checked
        this.getContent()
      })
    },
    // // 切换站点
    // handleChange(value) {
    //   // this.getColumnlist(this.siteId)
    //   this.getLang()
    //   // this.searchForm.cate_id = value[value.length - 1]
    // },
    // 栏目内容列表
    getContent() {
      const val = {
        page: this.page.currentPage,
        limit: this.page.size,
        category_id: this.category_id,
        lang: this.activeName,
        website_id: this.siteId
      }
      columnApi.contentList(val).then(res => {
        this.contentData = res.data
        this.page.total = res.count
        this.list = []
        // 表格数据  drawerTableList
        // 获取每张图片的地址
        for (var x = 0;x < this.contentData.length;x++) {
          if (this.contentData[x].thumbnail != null) {
            this.list.push(this.contentData[x].thumbnail.url)
          }
        }

        // 根据图片顺序（index）更改每张图片绑定的list的图片顺序
        for (var i = 0;i < this.contentData.length;i++) {
        // this.list.push(this.drawerTableList[i].path)
          var container = this.list

          var frontArr = container.slice(0, i)
          var behindArr = container.slice(i, this.contentData.length)
          var list = behindArr.concat(frontArr)
          this.contentData[i].list = list
        }
      })
    },
    getModule() {
      contentApi.moduleList({ website_id: this.website_id }).then(res => {
        this.moduleData = res.data
      })
    },
    // 查看副表
    handleCommand(val) {
      this.$router.push({ name: 'ColumnInfo', query: { id: val, website_id: this.siteId }})
    },

    // 新增
    goAdd() {
      if (this.node.length > 1) {
        this.$message.error('只能选中一个栏目')
      } else if (this.node.length == 1) {
        if (this.node[0].module_id) {
          this.$router.push({ name: 'addContent', query: { website_id: this.siteId, lang: this.activeName, module_id: this.node[0].module_id, category_id: this.node[0].id }})
          // this.$message.success('一个节点，并且包含模型')
        } else {
          this.$message.error('此栏目无法新增内容,请更换其他栏目')
        }
      } else {
        this.$message.error('请选择一个栏目')
      }
      // return
    },
    // 编辑
    editor(row) {
      const val = {
        id: row.id,
        module_id: row.module_id,
        lang: this.activeName,
        website_id: this.siteId
      }
      columnApi.contentInfo(val).then(res => {
        if (res.code == 0) {
          const website = res.data.content.website
          const category = res.data.content.category

          this.$router.push({
            name: 'updateContent',
            query: {
              id: row.id,
              category_id: category,
              module_id: row.module_id,
              lang: this.activeName,
              siteId: website }
          })
        }
      })
    },
    // 查看
    check(row) {
      this.$router.push({ name: 'ColumnInfo', query: { id: this.siteId }})
    },
    // 删除
    del(row) {
      this.$confirm('此操作将删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          columnApi.contentDelete({ module_id: row.module_id, id: row.id }).then((res) => {
            if (res.code == 0) {
              this.getContent()
              this.$message.success(res.msg)
            } else {
              this.$message.error(res.msg)
            }
          })
            .catch((res) => {
              this.$message.error(res.msg)
            })
        })
        .catch((rej) => {

        })
    },

    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val
      this.page.currentPage = 1
      this.getContent()
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getContent()
    }

  }
}
</script>
<style lang='scss'>
#order{
 .el-date-editor.el-input.el-date-editor--date{
    width: 180px;
  }
}
  .tabs.el-tabs--card>.el-tabs__header .el-tabs__item{
  background-color: #eee;
  border: none;
  margin-right: 5px;
}
.tabs.el-tabs--card>.el-tabs__header .el-tabs__item.is-active{
  background-color: #fff;
}
.tabs.el-tabs--card>.el-tabs__header .el-tabs__nav{
  border-radius: 0;
  border: none;
}
.tabs.el-tabs--card .el-tabs__header{
  margin: 0;
}
.tabs.el-tabs--card>.el-tabs__header{
  border-bottom: 0;
}
</style>
<style scoped lang='scss'>
.content{
  background-color: #fff;
  padding-top: 20px;
}
    .el-table{
      margin-bottom: 20px;
    }
    .search{
      min-width: 1350px;
      background-color: #fff;
      padding: 35px 30px 20px;
      border-radius: 5px;
    }
    .flex{
      display: flex;
      .tree{
        width: 300px;
        margin-right: 20px;
        background-color: #fff;
        h4{
          padding:15px;
          border-bottom: solid 1px #eee;
          font-size: 15px;
        }
        .el-tree{
          padding: 15px;
        }
      }
    }
    .top{
      display: flex;
      h3.title{
        width: 320px;
      }
      .topbtn{
        margin-bottom: 10px;
        .el-dropdown-link {//模拟按钮
          cursor: pointer;
          color: #fff;
          background-color: #f84311;
          padding: 8.5px 17px;
          font-size: 12px;
          border-radius: 19px;
          margin-left: 15px;
        }
      }
    }
</style>
